package com.example.zhihuhelpapp.excel;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhihuhelpapp.R;
import com.example.zhihuhelpapp.adapter.QuestionFromExcelListAdapter;
import com.example.zhihuhelpapp.base.MyBaseRxActivity;
import com.example.zhihuhelpapp.bean.QuestionBean;
import com.example.zhihuhelpapp.bean.QuestionDataFormExcel;
import com.example.zhihuhelpapp.bean.QuestionResultBean;
import com.example.zhihuhelpapp.mvp.QuestionListPresenter;
import com.example.zhihuhelpapp.mvp.contract.IQuestionListContract;
import com.example.zhihuhelpapp.utils.CoreLib;
import com.example.zhihuhelpapp.utils.DbUtils;
import com.example.zhihuhelpapp.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LoadExcelDataActivity extends MyBaseRxActivity<QuestionListPresenter> implements IQuestionListContract.IView {
    private static final int REQUEST_CODE = 0x01;

    private List<QuestionDataFormExcel> mDatas;
    private QuestionFromExcelListAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_load_excel_data;
    }

    @Override
    protected void initView() {
        showToolBar(true, "加载excel数据");
        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDatas = new ArrayList<>();
        mAdapter = new QuestionFromExcelListAdapter(this, mDatas);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected QuestionListPresenter createPresenter() {
        return new QuestionListPresenter();
    }

    public void chooseExcelFile(View view) {
        FileUtil.openFileSelector(this, REQUEST_CODE);
//        String filePath = "/storage/emulated/0/alipay/AndroidZhiHuAnswerData.xlsx";
//        try {
//            loadData(new File(filePath));
//        } catch (Exception e) {
//            e.printStackTrace();
//            Log.i("aijie", "程序异常：" + e.getMessage());
//        }
    }

    public void addAllToLocal(View view) {
        if (!mDatas.isEmpty()) {
            // 先删除本地所有已添加的
            DbUtils.delAll();
            for (QuestionDataFormExcel mData : mDatas) {
                if (CoreLib.isAnswer(mData.getUrl())) {
                    String answerId = CoreLib.getQuestionOrAnswerId(mData.getUrl());
                    mPresenter.getAnswerDetail(answerId);
                } else if (CoreLib.isQuestion(mData.getUrl())) {
                    String questionId = CoreLib.getQuestionOrAnswerId(mData.getUrl());
                    mPresenter.getQuestionDetail(questionId, 0);
                }
            }
        }
    }

    @Override
    public void onSaveQuestionSuccess() {

    }

    @Override
    public void onDelQuestionSuccess() {

    }

    @Override
    public void onGetLocalQuestionList(List<QuestionBean> questionBeanList) {

    }

    @Override
    public void onGetQuestionDetail(QuestionResultBean resultBean) {
        if (resultBean != null) {
            List<QuestionResultBean.DataBean> dataList = resultBean.getData();
            QuestionBean questionBean = new QuestionBean();
            questionBean.setQuestionAnswerCount(resultBean.getPaging().getTotals());
            if (dataList != null && dataList.size() > 0) {
                QuestionResultBean.DataBean dataBean = dataList.get(0);
                QuestionResultBean.DataBean.QuestionBean question = dataBean.getQuestion();
                questionBean.setQuestionId(String.valueOf(question.getId()));
                questionBean.setQuestionTitle(question.getTitle());
            }
            questionBean.setUpdateTime(System.currentTimeMillis());
            mPresenter.saveQuestionToLocal(questionBean);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                if (data == null) {
                    return;
                }
                Uri uri = data.getData();
                File file;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    file = FileUtil.uriToFileApiQ(uri, this);
                } else {
                    String path = FileUtil.getPath(this, uri);
                    Log.i("aijie", "path: " + path);
                    file = new File(path);
                }
                loadData(file);
            }
        }
    }

    private void loadData(File file) {
        if (file != null) {
            List<Map<String, String>> result = FileUtil.readExcel(file, new String[]{"标题", "阅读", "评论", "赞同", "感谢", "收藏", "链接"});
            if (result != null) {
                for (Map<String, String> map : result) {
                    if (map != null) {
                        QuestionDataFormExcel bean = new QuestionDataFormExcel(map.get("链接"), map.get("标题"), map.get("阅读"), map.get("评论"),
                                map.get("赞同"), map.get("感谢"), map.get("收藏"));
                        mDatas.add(bean);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }
    }
}
